﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;



namespace QMIM
{
    delegate void DelegateShowQQForm(QQMain qqmain);
    delegate void DelegateShowMSNForm();
    public partial class LoginForm : Form
    {
        private QQMain _qqMain;
        private MSNMain _msnMain;
        private LoginType _loginType;
        QQForm qqForm;
        MSNForm msnForm;
        public LoginForm()
        {
            InitializeComponent();
            qqForm = null;
            msnForm = null;
            Global.loginForm = this;
            _qqMain = new QQMain();
            _msnMain = new MSNMain();
            RegEvents();
        }

        #region 事件注册
        /// <summary>
        /// 注册QQ和MSN登陆事件
        /// </summary>
        private void RegEvents()
        {
            #region QQ事件
            _qqMain.LoginFailed += new EventHandler(LoginFailed);
            _qqMain.LoginSuccessed += new EventHandler(LoginSuccessed);
            #endregion

            #region MSN事件
            _msnMain.LoginFailed += new EventHandler(LoginFailed);
            _msnMain.LoginSuccessed += new EventHandler(LoginSuccessed);
            #endregion
        }
        #endregion

        private void LoginForm_Load(object sender, EventArgs e)
        {
            loginTypeCmb.SelectedIndex = (int)LoginType.QQ;
        }

        private void BtnLogin_Click(object sender, EventArgs e)
        {
            _loginType = (LoginType)loginTypeCmb.SelectedIndex;
            Global.loginType = _loginType;

            if (cboLoginName.Text.Trim() == "" || txtLoginPass.Text.Trim() == "")
            {
                return;
            }

            if (_loginType == LoginType.QQ)
            {
                _qqMain.DoLogin(int.Parse(cboLoginName.Text.Trim()), txtLoginPass.Text.Trim());
            }
            else if (_loginType == LoginType.MSN)
            {
                _msnMain.DoLogin(cboLoginName.Text.Trim(), txtLoginPass.Text.Trim());
            }
            else
            {
            }
            
        }

        private void LoginFailed(object sender, EventArgs e)
        {            
            this.Visible = true;
            MessageBox.Show("登陆失败", "提示");
        }

        private void LoginSuccessed(object sender, EventArgs e)
        {
            this.Visible = false;
            if (_loginType == LoginType.QQ)
            {
                ShowQQForm((QQMain)sender);
            }
            else if (_loginType == LoginType.MSN)
            {
                ShowMSNForm();
            }
        }

        /// <summary>
        /// 注意：由于事件是在子线程发生的，不能直接在子线程中创建窗口，而必须用委托和Invoke。
        /// </summary>
        /// <param name="qqmain"></param>
        private void ShowQQForm(QQMain qqmain)
        {
            if (this.InvokeRequired)
            {
                DelegateShowQQForm show = new DelegateShowQQForm(ShowQQForm);
                this.Invoke(show, new object[] { qqmain });
            }
            else
            {
                if (qqForm == null)
                {
                    qqForm = new QQForm();                   
                    qqForm.RegEvents();
                   
                }
                Global.qqForm = qqForm;
                qqForm.Show();
            }
        }

        private void ShowMSNForm()
        {
            if (this.InvokeRequired)
            {
                DelegateShowMSNForm show = new DelegateShowMSNForm(ShowMSNForm);
                this.Invoke(show, new object[] { });
            }
            else
            {
                if (msnForm == null)
                {
                    msnForm = new MSNForm();

                    //_msnMain.RegEvents();
                }
                Global.msnForm = msnForm;
                msnForm.Show();
            }
        }
    }
}
